﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using ExtremeML.Packaging;

namespace ExtremeML.Spreadsheet
{
	// This is a partial class that adds custom functionality to its auto-generated partner.
	public sealed partial class SheetWrapper
	{
		#region Custom Properties


		#endregion
		#region Custom Methods


		#endregion
		#region Overrides

		/// <summary>
		/// Validates changes to the name property to ensure uniqueness.
		/// </summary>
		partial void OnNamePropertyChanging(
			ref string modified,
			string original)
		{
			ValidateUniqueName((SpreadsheetDocumentWrapper)Package, modified);
		}

		#endregion
		#region Implementation Methods


		#endregion
		#region Static Methods

		/// <summary>
		/// Validates the uniqueness of the passed worksheet name and throws an exception accordingly.
		/// </summary>
		/// <param name="package">The <see cref="SpreadsheetDocumentWrapper"/> in which to perform validation.</param>
		/// <param name="name">The worksheet name to be validated.</param>
		internal static void ValidateUniqueName(
			SpreadsheetDocumentWrapper package,
			string name)
		{
			if (package.WorkbookPart.Workbook.Sheets.Sheets[name] != null)
			{
				throw new ArgumentException(string.Format("A worksheet with the name {0} already exists.", name));
			}
		}

		#endregion
	}
}
